<script setup>
import topNav from "../../components/topNav.vue";
import { introApi } from "@/services/index";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { nextTick, ref, watch, onMounted } from "vue";
import { appInfo, loginSwitch } from "@/services/user";
// import topNav from "../../components/topNav.vue";
import { operation } from "@/utils/num.js";
import {
  appScTransferDetail,
  scTransferSign,
  scTransferRemind,
  appScTransferReturnQuery,
} from "@/services/transfer";
import { getPlan, getTakeSheetDetail } from "@/services/stocktaking";

const showSelect = ref("");

if (uni.getStorageSync("selectShopType")) {
  showSelect.value = uni.getStorageSync("selectShopType");
}

const id = ref("");
const type = ref(1);
onLoad((e) => {
  id.value = e.id;
  type.value = e.type ? e.type : 1;
  getAppScTransferDetail();
  // getPlanConfig();

  // getAppScTransferReturnQuery();
});
onShow(() => {
  // getAppScTransferReturnQuery();
});

const optionDet = ref({});
const returnList = ref([]);
const loading = ref(true);
const allowChangeNum = ref(false);
const autoChangeStock = ref(false);

const getPlanConfig = async () => {
  loading.value = true;
  try {
    let res = await planConfig({ id: id.value });
    allowChangeNum.value = res.data.allowChangeNum;
    autoChangeStock.value = res.data.autoChangeStock;
  } catch (e) {}
};
const getAppScTransferDetail = async () => {
  loading.value = true;
  try {
    let res = await getTakeSheetDetail({ id: id.value });
    optionDet.value = res.data;
    numgoods.value = res.data.details.length;
    loading.value = false;
  } catch (e) {}
};

const getAppScTransferReturnQuery = async () => {
  try {
    let res = await appScTransferReturnQuery({ id: id.value });
    returnList.value = res.data;
  } catch (e) {}
};

//签收

const passwordShow = ref(false);
const isshowpassword = ref(false);
// const selectItem = ref({});
const password = ref("");

const showQs = () => {
  passwordShow.value = true;
  isshowpassword.value = false;
  password.value = "";
  // selectItem.value = item;
};
const isUp = ref(false);
const qrUpSign = async () => {
  if (!password.value) {
    uni.showToast({
      title: "请输入登录密码",
      icon: "none",
    });
    return;
  }

  if (isUp.value) {
    return;
  }
  isUp.value = true;
  try {
    let res = await scTransferSign({
      id: id.value,
      password: password.value,
    });
    if (res.code == 200) {
      uni.showToast({
        title: "签收成功",
        icon: "none",
      });
      optionDet.value.status = 12;
      isUp.value = false;
      passwordShow.value = false;

      // getAppProductQuery(1);
    } else {
      uni.showToast({
        title: res.msg,
        icon: "none",
      });
      isUp.value = false;
    }

    console.log(res);
  } catch (e) {
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
    isUp.value = false;
  }
};

const toSign = async () => {
  passwordShow.value = true;
  // try {
  //   let res = await scTransferRemind({
  //     id: id.value,
  //   });
  //   if (res.code == 200) {
  //     uni.showToast({
  //       title: "您已发送提醒",
  //       icon: "success",
  //     });
  //   } else {
  //     uni.showToast({
  //       title: res.msg,
  //       icon: "none",
  //     });
  //   }
  //   console.log(res);
  // } catch (e) {
  //   uni.showToast({
  //     title: e.msg,
  //     icon: "none",
  //   });
  // }
};

const showModal = ref(false);
const upData = async (index) => {
  try {
    let res = await planDiff({
      id: id.value,
      type: index,
    });
    if (res.code == 200) {
      // uni.showToast({
      //   title: "",
      //   icon: "success",
      // });
      showModal.value = true;
      passwordShow.value = false;
    } else {
      uni.showToast({
        title: res.msg,
        icon: "none",
      });
    }
    console.log(res);
  } catch (e) {
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
  }
};
const toReturn = async () => {
  let arr = [];
  optionDet.value.details.forEach((item) => {
    arr.push({ productId: item.productId, takeNum: item.oriTakeNum });
  });
  console.log(arr);
  try {
    let res = await planHandle({
      id: id.value,
      // type: index,
      products: arr,
      allowChangeNum: allowChangeNum.value,
      autoChangeStock: autoChangeStock.value,
    });
    if (res.code == 200) {
      // uni.showToast({
      //   title: "",
      //   icon: "success",
      // });
      showModal.value = true;
      passwordShow.value = false;
    } else {
      uni.showToast({
        title: res.msg,
        icon: "none",
      });
    }
    console.log(res);
  } catch (e) {
    uni.showToast({
      title: e.msg,
      icon: "none",
    });
  }
};
const goStock = () => {
  showModal.value = false;

  const pages = getCurrentPages();

  if (pages.length > 1) {
    uni.navigateBack({ delta: 1 });
  } else {
    uni.redirectTo({
      url: "/pages/stocktaking/add_list/index",
    });
  }
};

const copy = (item) => {
  uni.setClipboardData({
    data: item,
    success: function () {
      uni.showToast({
        title: "复制成功",
        icon: "none",
      });
    },
  });
};

const selectOne = ref(true);
const selectTwo = ref(true);
const selectThree = ref(true);
const numgoods = ref(0);
const toDetail = (index) => {
  let num = 0;
  if (index == 1) {
    selectOne.value = !selectOne.value;
  }
  if (index == 2) {
    selectTwo.value = !selectTwo.value;
  }
  if (index == 3) {
    selectThree.value = !selectThree.value;
  }

  optionDet.value.details.forEach((item) => {
    if (
      (selectOne.value && item.stockNum > item.oriTakeNum) ||
      (selectTwo.value && item.stockNum < item.oriTakeNum) ||
      (selectThree.value && item.stockNum == item.oriTakeNum) ||
      (!item.oriTakeNum && selectThree.value)
    ) {
      num = num + 1;
    }
  });
  console.log(num);
  numgoods.value = num;
};

//无网状态
const connected = () => {
  getAppScTransferDetail();
};

//提醒收货
</script>

<template>
  <view class="bg">
    <topNav
      :title="'盘点详情'"
      @save="saveFun"
      :border="true"
      rType="save"
    ></topNav>

    <!-- <view class="loading" v-if="loading">
      <view class="loading_box">
        <up-icon
          class="reload"
          name="reload"
          color="#a5a5a5"
          size="100rpx"
        ></up-icon>
      </view>

      <view class="loading_text">加载中</view>
    </view> -->
    <view class="state" v-if="!loading">
      <view class="state_box">盘点信息</view>
      <view class="state_line">
        <image src="@/static/cashier/yespage.png" mode="scaleToFill" />
      </view>
    </view>

    <view class="top" v-if="!loading">
      <view class="top_title">
        <text> {{ optionDet.name }}</text>
      </view>
      <view class="top_dn"
        ><text class="text_left">仓库</text>
        <text>{{ optionDet.scName }}</text>
      </view>
      <view class="top_dn"
        ><text class="text_left">盘点类型</text>
        <text>{{
          optionDet.takeType == 0
            ? "全部盘点"
            : optionDet.takeType == 1
            ? "单品盘点"
            : optionDet.takeType == 2
            ? "分类盘点"
            : "品牌盘点"
        }}</text>
      </view>
      <!-- <view class="top_dn"
        ><text class="text_left">盘点内容</text>
        <text>{{ optionDet.name }}</text>
      </view> -->

      <view class="top_dn"
        ><text class="text_left">数据编号</text>
        <text
          >{{ optionDet.code }}
          <text class="copy" @click="copy(optionDet.code)">复制</text></text
        >
      </view>
      <view class="top_dn"
        ><text class="text_left">盘点时间</text>
        <text>{{ optionDet.approveTime }}</text>
      </view>
    </view>

    <view class="info" v-if="!loading">
      <view class="info_title"
        ><text>明细</text>
        <!-- <view class="info_title_right">
          <view
            class="info_title_right_item"
            :class="selectOne ? 'active' : ''"
            @click="toDetail(1)"
          >
            报损商品
          </view>
          <view
            class="info_title_right_item"
            :class="selectTwo ? 'active' : ''"
            @click="toDetail(2)"
          >
            报溢商品
          </view>
          <view
            class="info_title_right_item"
            :class="selectThree ? 'active' : ''"
            @click="toDetail(3)"
          >
            无报溢商品
          </view>
        </view> -->
      </view>
      <!-- <view class="info_dh">
        <text
          ><text class="color">调货人:</text> {{ optionDet.createBy }} 调给
          {{ optionDet.approveBy }}</text
        >
        <text><text class="color">时间:</text>{{ optionDet.approveTime }}</text>
      </view> -->
      <view class="info_list">
        <view class="info_item title">
          <view class="info_item_title">商品名称</view>
          <view class="info_item_num">库存</view>
          <view class="info_item_num">盘点数</view>
          <!-- <view class="info_item_uit">差异数</view> -->
          <view class="info_item_price">商品编号</view>
        </view>
        <!-- v-if="
            (selectOne && item.stockNum > item.oriTakeNum) ||
            (selectTwo && item.stockNum < item.oriTakeNum) ||
            (selectThree && item.stockNum == item.oriTakeNum)
          " -->
        <view v-for="(item, index) in optionDet.details" :key="index">
          <view
            class="info_item texts"
            v-if="
              (selectOne && item.stockNum > item.oriTakeNum) ||
              (selectTwo && item.stockNum < item.oriTakeNum) ||
              (selectThree && item.stockNum == item.oriTakeNum) ||
              (!item.oriTakeNum && selectThree)
            "
          >
            <view class="info_item_title"
              ><text>{{ item.productName }}</text>
            </view>
            <view class="info_item_num">{{ item.stockNum }}</view>

            <view class="info_item_num">{{ item.takeNum }}</view>

            <!-- <view class="info_item_uit">{{ item.diffNum }}</view> -->
            <view class="info_item_price">
              {{ item.takeNum }}
              <!-- <uni-tooltip
              class="item"
              :content="item.productCode"
              placement="left"
            >
              <view class="item">{{ item.productCode }}</view>
            </uni-tooltip> -->
            </view>
          </view>
        </view>
      </view>
      <view class="info_item-fo">
        <view class="info_item_left">共 {{ numgoods }} 款</view>
        <!-- <view class="info_item_center">调货总价:</view>
        <view class="info_item_right">¥{{ optionDet.totalAmount }}</view> -->
      </view>
    </view>

    <view style="width: 100%; height: 150rpx"></view>
  </view>
  <!-- v-if="type == 1" -->

  <!-- v-permission="['shop:scTransfer:remind', 'shop:scTransfer:return:create']" -->

  <up-modal
    width="600rpx"
    confirmText="返回"
    :show="showModal"
    :title="'提交成功'"
    @confirm="goStock"
  ></up-modal>
  <noWork @connected="connected"> </noWork>
</template>

<style lang="scss" scoped>
@import "index.scss";
</style>
